package com.hibase.upms.service.rolemenu.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hibase.common.constant.RedisKeyConstant;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import com.hibase.core.redis.util.RedisUtil;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.upms.entity.rolemenu.MdsRoleMenu;
import com.hibase.upms.entity.rolemenu.RolePermissions;
import com.hibase.upms.mapper.rolemenu.MdsRoleMenuMapper;
import com.hibase.upms.service.rolemenu.MdsRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ChrisCui
 * @since 2019-04-19
 */
@Service
public class MdsRoleMenuServiceImpl extends BaseServiceImpl<MdsRoleMenuMapper, MdsRoleMenu> implements MdsRoleMenuService {

//    @Autowired
//    private IMdsRoleMenuFilterService mdsRoleMenuFilterService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MdsRoleMenuMapper mdsRoleMenuMapper;
    
    
    /**
     * 查询角色-菜单数据
     */
    @Override
    public MdsRoleMenu selectMdsRoleMenu(String menuId, String roleId) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("menu_id", menuId);
        queryWrapper.eq("role_id", roleId);

        // 校验角色是否有效
        MdsRoleMenu roleMenu = this.getOne(queryWrapper, true);

        HibaseAssert.notNull(roleMenu, "service.mds.rolemenu.impl.mdsRoleMenuServiceImpl.selectMenuPermission");

        // 获取选中的数据项
//        List<MdsRoleMenuFilter> mdsRoleMenuFilters = mdsRoleMenuFilterService.list(new QueryWrapper<MdsRoleMenuFilter>().eq("role_menu_id", roleMenu.getId()));

        List<String> filterIds = new ArrayList<>();

//        for (MdsRoleMenuFilter mdsRoleMenuFilter : mdsRoleMenuFilters) {
//
//            filterIds.add(mdsRoleMenuFilter.getFilterId());
//        }

        roleMenu.setFilterIds(filterIds);

        return roleMenu;
    }

    @Override
    public List<RolePermissions> selectRolePermissions() {

        List<RolePermissions> rolePermissionsList = this.baseMapper.selectRolePermissions();
        return rolePermissionsList;

    }

    @Override
    public void refresh() {
        List<RolePermissions> rolePermissionsList = this.selectRolePermissions();
        // key url  value role list
        Map<String, List<String>> rolePermissionsMap = rolePermissionsList.stream().collect(Collectors.groupingBy(RolePermissions::getUrl, Collectors.mapping(
                RolePermissions::getSysCode, Collectors.toList())));
        redisUtil.delete(RedisKeyConstant.CACHE_USER_ROLE_KEY);
        redisUtil.hPutAll(RedisKeyConstant.CACHE_USER_ROLE_KEY,rolePermissionsMap);
    }

    /**
     * (non-Javadoc)
     * <p>Title: getListByRoleId</p>
     * <p>Description: 根据roleId查询按钮权限</p>
     * @author TimBrian
     * @since Aug 3, 2021 5:03:40 PM
     * @param id
     * @return
     * @see MdsRoleMenuService#getListByRoleId(java.lang.String)
     */
	@Override
	public List<MdsRoleMenu> getListByRoleId(String id) {
		return mdsRoleMenuMapper.getListByRoleId(id);
	}

	/**
	 * (non-Javadoc)
	 * <p>Title: seleParentIdById</p>
	 * <p>Description: 根据ID获取父级ID</p>
	 * @author TimBrian
	 * @since 2021年9月28日 上午10:54:43
	 * @param ids
	 * @return
	 * @see MdsRoleMenuService#seleParentIdById(java.util.List)
	 */
	@Override
	public Set<String> seleParentIdById(List<String> ids) {
		return mdsRoleMenuMapper.seleParentIdById(ids);
	}
}
